package com.yycx.module.bus.provider.handler.haiying.tasktodo.settle.invoice;

import com.yycx.common.base.entity.EntityMap;
import com.yycx.common.base.utils.FlymeUtils;
import com.yycx.common.constants.CommonConstants;
import com.yycx.common.mybatis.interceptor.SaveInterceptor;
import com.yycx.common.mybatis.interceptor.UpdateInterceptor;
import com.yycx.common.mybatis.query.CriteriaSave;
import com.yycx.common.mybatis.query.CriteriaUpdate;
import com.yycx.common.security.OpenHelper;
import com.yycx.common.utils.ApiAssert;
import com.yycx.common.utils.OrderNoUtil;
import com.yycx.common.utils.RedisUtils;
import com.yycx.module.bus.client.entity.BusDept;
import com.yycx.module.bus.client.entity.BusInvoiceunit;
import com.yycx.module.bus.client.entity.BusPayment;
import com.yycx.module.bus.client.enums.AmountTypeEnum;
import com.yycx.module.bus.provider.service.BusDeptService;
import com.yycx.module.bus.provider.service.BusInvoiceunitService;
import com.yycx.module.bus.provider.service.BusPaymentService;
import com.yycx.module.bus.provider.service.hy.HyOrderDetailService;
import com.yycx.module.bus.provider.service.hy.HyOrderService;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.math.RoundingMode;

/**
 * 开票更新扩展器
 *
 * @author zyf
 */
@Component
public class HyInvoicePaymentUpdateHandler implements UpdateInterceptor<BusPayment> {


    @Resource
    private BusPaymentService paymentService;
    @Resource
    private BusDeptService deptService;

    @Resource
    private BusInvoiceunitService invoiceunitService;

    @Resource
    private HyOrderDetailService orderDetailsService;

    @Resource
    public RedisUtils redisUtils;


    @Override
    public void prepare(CriteriaUpdate cs, EntityMap entityMap, BusPayment payment) {
        Long paymentId = payment.getPaymentId();
        BusPayment old = paymentService.getById(paymentId);
        //结算金额
        BigDecimal amount = payment.getAmount();
        ApiAssert.isNotEmpty("结算金额不能为空", amount);
        //是否包票1包 0不包
        Integer baopiao = entityMap.getInt("baopiao", 1);
        //发票类型(2:专票 1:普票)
        Integer invoiceType = entityMap.getInt("invoiceType", 2);
        //开票金额
        BigDecimal invoiceAmount = new BigDecimal("0");
        //税金金额
        BigDecimal taxAmount = new BigDecimal("0");
        //不含税金额
        BigDecimal noTaxAmount = new BigDecimal("0");
        //二次开票金额
        BigDecimal senondInvoiceAmount = new BigDecimal("0");
        //二次税金
        BigDecimal secondTaxAmount = new BigDecimal("0");
        EntityMap config = redisUtils.getConfigMap("DBR_CONFIG");
        //税率
        BigDecimal tax = FlymeUtils.getBigDecimal(config.getBigDecimal("tax"), "1.03");
        //开票单位ID
        Long invoiceUnitId = payment.getInvoiceUnitId();
        BusInvoiceunit invoiceUnit = invoiceunitService.getById(invoiceUnitId);
        ApiAssert.isNotEmpty("开票单位不存在", invoiceUnit);
        BigDecimal taxRate = new BigDecimal(invoiceUnit.getTaxNo());
        EntityMap totalDeptSettleMap = orderDetailsService.totalSettleAmount(old.getSelectIds(),old.getSettleType());
        Long deptId = totalDeptSettleMap.getLong("deptId");
        BusDept dept = deptService.getById(deptId);
        ApiAssert.isNotEmpty("部门不存在", invoiceUnit);
        BigDecimal invoiceRate = FlymeUtils.getBigDecimal(dept.getInvoiceRate(), "1.0423");
        //包票
        if (baopiao.equals(1)) {
            //开票金额(个位取整)
            invoiceAmount = getRoundAmount(amount.multiply(invoiceRate));
            //二次开票金额
            senondInvoiceAmount = getRoundAmount(invoiceAmount.divide(tax, 2, BigDecimal.ROUND_HALF_UP).multiply(new BigDecimal("0.03")).multiply(invoiceRate));
            //二次税金
            secondTaxAmount = senondInvoiceAmount.divide(tax, 2, BigDecimal.ROUND_HALF_UP).multiply(taxRate).setScale(2, BigDecimal.ROUND_HALF_UP);

        } else {
            //专票
            if (invoiceType.equals(2)) {
                invoiceAmount = getRoundAmount(amount.multiply(tax));
            }
            //普票
            if (invoiceType.equals(1)) {
                //开票金额=结算金额
                invoiceAmount = amount;
            }
        }
        noTaxAmount = invoiceAmount.divide(tax, 2, BigDecimal.ROUND_HALF_UP).setScale(2, BigDecimal.ROUND_HALF_UP);
        //税金
        taxAmount = invoiceAmount.divide(tax, 2, BigDecimal.ROUND_HALF_UP).multiply(taxRate).setScale(2, BigDecimal.ROUND_HALF_UP);
        payment.setInvoiceAmount(invoiceAmount);
        payment.setTaxAmount(taxAmount);
        payment.setNoTaxAmount(noTaxAmount);
        payment.setSecondTaxAmount(secondTaxAmount);
        payment.setSenondInvoiceAmount(senondInvoiceAmount);
    }

    @Override
    public void complete(CriteriaUpdate cs, EntityMap params, BusPayment entity) {

    }

    private BigDecimal getRoundAmount(BigDecimal invoiceAmount) {
        return invoiceAmount.divide(new BigDecimal(10)).setScale(0, BigDecimal.ROUND_HALF_UP).multiply(new BigDecimal("10"));
    }

}
